package com.backend.werewolfkillingbackend.interceptor;

import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import java.util.Map;

@Component
public class ChatHandshakeInterceptor implements HandshakeInterceptor {
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
                                   Map<String, Object> attributes) throws Exception {
        // 验证用户身份并将用户名存储到session的attributes中

        ServletServerHttpRequest servletRequest = (ServletServerHttpRequest) request;

        // 从请求中获取参数
        String roomId = servletRequest.getServletRequest().getParameter("roomId");
        String username = servletRequest.getServletRequest().getParameter("username");

        attributes.put("roomId", roomId);
        attributes.put("username", username);
        return true;
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
                               Exception exception) {
        // 握手之后的处理逻辑
    }

}
